const host = 'https://testnp.napin.com/'
function getRequest (url, method, data, header, isToLogin) {
  wx.showLoading({
    title: '加载中' // 数据请求前loading
  })
  if (isToLogin) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: host + url,
        method: method,
        data: data,
        success: function (res) {
          if (res.data.code == 1) {
            wx.hideLoading()
            resolve(res.data)
          } else if (res.data.code == -2) {
            wx.hideLoading()
            let url='../../pages/login/main'
            wx.navigateTo({ url })
          }else{
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000,
              mask:true
            })
          }
        },
        fail: function (res) {
          wx.hideLoading()
          resolve(res.data)
          // reject(false)
        },
        complete: function () {
          wx.hideLoading()
        }
      })
    })
  }else{
    return new Promise((resolve, reject) => {
      wx.request({
        url: host + url,
        method: method,
        data: data,
        success: function (res) {
          wx.hideLoading()
          resolve(res.data)
          if(res.data.code == 1){

          }else{
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 2000,
              mask:true
            })
          }
        },
        fail: function (res) {
          wx.hideLoading()
          resolve(res.data)
          // reject(false)
        },
        complete: function () {
          wx.hideLoading()
        }
      })
    })
  }

}
export default {
  getRequest
}
